﻿<?php
include 'test_header.php';

class test_user_signin_and_singout extends WebTestCase{
	
	function setUp() {
		//重启浏览器
		$this->restart();

		$db = db_connect();
		$sql = "
INSERT INTO `soj`.`user` (`username`, `passwd`, `role`, `email`, `signup_time`, `last_signin_time`, `last_signin_ipadd`) VALUES
('judas', '202cb962ac59075b964b07152d234b70', '0', '', '2010-05-08', NULL, NULL)
";
		$res = $db->query( $sql );
	}
	function tearDown(){ 
		
		$db = db_connect();
		$sql = 'delete from `soj`.`user` where username=`judas`';
		$res = $db->query( $sql );
	}


	function test_user_sigin_success_without_captcha(){


		$this->get( 'http://127.0.0.1/soj/php/account/signin.php' );
		$this->setField( 'username' , 'judas' );
		$this->setField( 'passwd' , '123' ) ;

		//登录成功 success_handle显示success字样
		$this->click('Sign in');
		$this->assertText( 'success' );


	}
	function test_when_user_signin_fail_should_show_captcha(){

		$this->get( 'http://127.0.0.1/soj/php/account/signin.php' );
		$this->setField( 'username' , 'judas' );
		$this->setField( 'passwd' , '321' ) ;

		$this->click('Sign in');
		$this->assertText( 'Enter above phase' );
		$this->restart();

	}
	function test_user_signin_fail_with_captcha(){
	
		$this->get( 'http://127.0.0.1/soj/php/account/signin.php' );
		$this->setField( 'username' , 'judas' );
		$this->setField( 'passwd' , '321' ) ;
		$this->click('Sign in');
		$this->assertText( 'Enter above phase' );

		$this->setField( 'username' , 'judas' );
		$this->setField( 'passwd' , '321' ) ;
		$this->setField( 'captcha' , 'xxxxx' ) ;
		$this->click('Sign in');

		$this->assertText( 'Invalid captcha value.' );

		//刷新后,captcha错误提示应该消失
		$this->retry();
		$this->assertNoText( 'Invalid captcha value.' );
	}
	function test_user_signout(){

		$this->get( 'http://127.0.0.1/soj/php/account/signin.php' );
		$this->setField( 'username' , 'judas' );
		$this->setField( 'passwd' , '123' ) ;
		$this->click('Sign in');
		$this->assertText( 'Sign in success' );

		$this->clickLinkById( 'signout' ) ;
		$this->assertText( 'Sign out success' );		
	}
	function test_if_user_visit_signout_page_without_signin(){
		//重定向到index.php	
	}
	function test_set_cookie(){}
}

$test = new test_user_signin_and_singout();
$test->run( new HtmlReporter() );

